package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.role.*;
import com.cskaoyan.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleSuperviseController {


    @Autowired
    RoleService roleService;

    @RequestMapping("/options")
    public BaseRespVo options() {
        List<RoleOptionVO> roleOptionVOList = roleService.options();
        return BaseRespVo.ok(roleOptionVOList);
    }

    @RequestMapping("/list")
    public BaseRespVo list(Integer page,Integer limit,String name,String sort,String order) {
        RoleListVO roleListVO = roleService.list(page,limit,name,sort,order);
        return BaseRespVo.ok(roleListVO);
    }

    @RequestMapping("/create")
    public BaseRespVo create(@RequestBody Role role) {
        Role role1 = roleService.create(role);
        if (role1.getName() == null) {
            return BaseRespVo.fail(640,"角色已经存在");
        }
        return BaseRespVo.ok(role1);
    }

    @RequestMapping("/update")
    public BaseRespVo update(@RequestBody Role role) {
        if (role.getId() == 1) {
            return BaseRespVo.fail(641,"当前超级角色的信息不能变更");
        }
        int result = roleService.update(role);
        if (result == 1) {
            return BaseRespVo.ok();
        }
        else
            return BaseRespVo.fail(640,"角色已存在");
    }

    @RequestMapping("/delete")
    public BaseRespVo delete(@RequestBody Role role) {
        if (role.getName().equals("超级管理员"))
            return BaseRespVo.fail(641,"当前超级角色的信息不能变更");
        roleService.delete(role);
        return BaseRespVo.ok();
    }

    /**
     * 获取systemPermissions
     * @param roleId
     * @return
     */
//    @RequestMapping("store")
//    public BaseRespVo store(@RequestBody SystemPermissions systemPermissions) {
//        roleService.store(systemPermissions);
//        return BaseRespVo.ok();
//    }

    @GetMapping("permissions")
    public BaseRespVo permissions(Integer roleId) {
        PermissionBean permissionBean = roleService.permissions(roleId);
        return BaseRespVo.ok(permissionBean);
    }
    @PostMapping("permissions")
    public BaseRespVo permissions(@RequestBody PermissionBO permissionBO) {
        int result = roleService.permissions2(permissionBO);
        if (result == 0) {
            return BaseRespVo.fail(641,"当前角色的超级权限不能变更");
        }
        return BaseRespVo.ok();
    }
}
